package com.xhwl.logistics.converter;

import com.xhwl.common.enums.vehicle.ReleaseTypeEnum;
import com.xhwl.common.pojo.cent.vehicle.VehcCarRecord;
import com.xhwl.logistics.bo.VehicleAccessRecordBO;
import com.xhwl.logistics.component.VehicleConfigProperty;
import com.xhwl.logistics.component.VehicleHomeAddressProperty;
import com.xhwl.logistics.enums.VehiclePassTypeEnum;
import com.xhwl.logistics.util.AboutDateUtil;
import com.xhwl.logistics.vo.VehicleAccessRecordAnotherVO;
import com.xhwl.logistics.vo.VehicleAccessRecordVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

//@Component
public class ToVehicleAccessRecordVO {

    @Autowired
    private VehicleHomeAddressProperty vehicleHomeAddressProperty;

    @Autowired
    private VehicleConfigProperty vehicleConfigProperty;

    private boolean judgeOvernight(Date in, Date out) {
        if (null!=in && null!=out) {
            Date overnightDate = AboutDateUtil.overnightDate(in, vehicleConfigProperty.getZone(), vehicleConfigProperty.getDay(), vehicleConfigProperty.getHour(), vehicleConfigProperty.getMinute());
            return out.getTime() > overnightDate.getTime();
        }
        return false;
    }

    public VehicleAccessRecordVO toVehicleAccessRecordVO(VehcCarRecord vehcCarRecord) {
        VehicleAccessRecordVO vehicleAccessRecordVO = new VehicleAccessRecordVO();

        BeanUtils.copyProperties(vehcCarRecord, vehicleAccessRecordVO);

        //车辆归属地
        //增加了脏数据的判断，为了测试，后续需要修改
        String plateNumber = vehcCarRecord.getPlateNumber();
        if (null==plateNumber||plateNumber.length()<2) {
            vehicleAccessRecordVO.setHomeAddress("nothing");
        } else {
            vehicleAccessRecordVO.setHomeAddress(vehicleHomeAddressProperty.getHomeAddress(vehcCarRecord.getPlateNumber().substring(0, 2)));
        }

        //车辆是否过夜车
        if (judgeOvernight(vehcCarRecord.getEnterTime(), vehcCarRecord.getExistTime())) {
            vehicleAccessRecordVO.setOvernight(Boolean.TRUE);
        } else {
            vehicleAccessRecordVO.setOvernight(Boolean.FALSE);
        }

        //车辆放行类型
        ReleaseTypeEnum releaseTypeEnum = vehcCarRecord.getExistType();
        if (null!=releaseTypeEnum) {
            vehicleAccessRecordVO.setPassType(VehiclePassTypeEnum.toVehiclePassTypeEnum(releaseTypeEnum.getKey()).getDescription());
        }

        //测试用的（后续需要删除）
        Date enterTime = vehcCarRecord.getEnterTime();
        Date existTime = vehicleAccessRecordVO.getExistTime();
        System.out.println("=================");
        System.out.println("id: " + vehcCarRecord.getId());
        System.out.println("enterTime: " + enterTime);
        System.out.println("existTime: " + existTime);
        System.out.println("=================");

        return vehicleAccessRecordVO;
    }

    public List<VehicleAccessRecordVO> toVehicleAccessRecordVOList(List<VehcCarRecord> list) {
        return list.stream().map(this::toVehicleAccessRecordVO).collect(Collectors.toList());
    }

    public VehicleAccessRecordAnotherVO toVehicleAccessRecordAnotherVO(VehicleAccessRecordBO vehicleAccessRecordBO) {
        VehicleAccessRecordAnotherVO vehicleAccessRecordAnotherVO = new VehicleAccessRecordAnotherVO();

        BeanUtils.copyProperties(vehicleAccessRecordBO, vehicleAccessRecordAnotherVO, "enterTime", "exitTime", "paidMoney");

        Date enterTime = vehicleAccessRecordBO.getEnterTime();
        Date exitTime = vehicleAccessRecordBO.getExitTime();
        if (null!=enterTime) {
            vehicleAccessRecordAnotherVO.setEnterTime(AboutDateUtil.dateToString(enterTime));
        }
        if (null!=exitTime) {
            vehicleAccessRecordAnotherVO.setExitTime(AboutDateUtil.dateToString(exitTime));
        }

        vehicleAccessRecordAnotherVO.setPaidMoney(vehicleAccessRecordBO.paidMoneyString());

        String plateNumber = vehicleAccessRecordBO.getPlateNumber();
        if (null==plateNumber || plateNumber.length()<2) {
            vehicleAccessRecordAnotherVO.setHomeAddress("/");
        } else {
            vehicleAccessRecordAnotherVO.setHomeAddress(vehicleHomeAddressProperty.getHomeAddress(plateNumber.substring(0, 2)));
        }

        vehicleAccessRecordAnotherVO.setOvernight(judgeOvernight(vehicleAccessRecordBO.getEnterTime(), vehicleAccessRecordBO.getExitTime()));

        vehicleAccessRecordAnotherVO.setStayTime(AboutDateUtil.durationString(exitTime, enterTime));

        return vehicleAccessRecordAnotherVO;
    }

    public List<VehicleAccessRecordAnotherVO> toVehicleAccessRecordAnotherVOList(List<VehicleAccessRecordBO> vehicleAccessRecordBOList) {
        return vehicleAccessRecordBOList.stream().map(this::toVehicleAccessRecordAnotherVO).collect(Collectors.toList());
    }

}
